<?php
	/*==================================================================*/
	/*		文件名:imageAction.php                                    */
	/*		概要: 图片管理         */
	/*==================================================================*/
	class ImageAction extends CommonAction {
		private $album;
		private $image;
		function __construct(){
			parent::__construct();
			$this->album=new AlbumModel();
			$this->image=new ImageModel();
		}
		function  add($tmess=null,$status=0){
			if(empty($tmess)){
				$this->assign("tmess", '提示: 带<span class="red_font">*</span>的项目为必填信息.<br>可以上传的图片格式为GIF,JPEG和PNG,大小不能超过2M.  ');
			}else{
				$this->message($status,$tmess);
			}
			//记住选择的操作
			if(isset($_POST["jz"])){
				$this->assign("jz", "checked");
				$this->assign("select",$this->album->buildSelect("pid", $_POST["pid"]));
			}else{
				$this->assign("select",$this->album->buildSelect("pid"));
			}
			if($_POST["hasThumb"]){
				if($status)
					$this->assign("hasThumb", "");
				else
					$this->assign("hasThumb", "checked");
			}

			if($_POST["hasMark"]){
				if($status)
					$this->assign("hasMark", "");
				else
					$this->assign("hasMark", "checked");
			}
			$this->display("admin/image/add.tpl");
		}

		function insert(){
			if($this->image->addPic($_POST)){
				$this->add($this->image->getMessList(),1);
			}else{
				$this->add($this->image->getMessList(),0);
			}
		}

		function edit($tmess=null,$status=0){
			if(empty($tmess)){
				$this->assign("tmess", '提示:如果想更改图片列表的显示方式请到系统管理的基本设置中进行修改.');
			}else{
				$this->message($status,$tmess);
			}
			

			$pid=isset($_REQUEST["pid"])?$_REQUEST["pid"]:1;
			$select=$this->album->buildSelect("pid",$pid, array("onChange"=>"window.location='admin.php?m=image&a=edit&pid='+options[selectedIndex].value"));
			$this->assign("select", $select);

			$page=new Page("admin.php?m=image&a=edit&pid=".$pid."&",$this->image->total("where pid={$pid}"), PICTURE_PAGE_SIZE);	 
			$images=$this->image->findAll(array("id, picTitle, picName, description,pid,hasThumb, hasMark"),$page->getOffset(), PICTURE_PAGE_SIZE, "where pid={$pid}");
			$this->assign("pics", $images);
			$this->assign("fpage", $page->getPage());
			$this->assign("pid", $pid);
			$cpage=isset($_GET["page"])?$_GET["page"]:1;
			$this->assign("page", $cpage);
			$this->assign("showType", PICTURE_SHOW_TYPE);
			$this->assign("path", GALLERY_PATH);	

			$this->display("admin/image/edit.tpl");
		}
		function del(){
			if($this->image->delPic($_REQUEST["id"])){
				$this->edit($this->image->getMessList(),1);
			}else{
				$this->edit($this->image->getMessList(),0);
			}

		}
		function open(){
			$pid=isset($_REQUEST["pid"])?$_REQUEST["pid"]:1;
			$select=$this->album->buildSelect("pid",$pid, array("onChange"=>"imglist('admin.php?m=image&a=listimg&pid='+options[selectedIndex].value)"));
			$this->assign("select", $select);
			$pid=isset($_GET["pid"])?$_GET["pid"]:1;
			$page=new AjaxPage("admin.php?m=image&a=listimg&pid={$pid}&",$this->image->total("where pid={$pid}"), PICTURE_PAGE_SIZE);	        
			$images=$this->image->findAll(array("id, picTitle, picName,pid"),$page->getOffset(), PICTURE_PAGE_SIZE, "where pid={$pid}");
			$this->assign("pics", $images);
			$this->assign("fpage", $page->getPage());
			$this->assign("path", GALLERY_PATH);
			$this->display("admin/image/open.tpl");	
		}

		function listimg(){
			$pid=isset($_REQUEST["pid"])?$_REQUEST["pid"]:1;
			$page=new AjaxPage("admin.php?m=image&a=listimg&pid={$pid}&",$this->image->total("where pid={$pid}"), PICTURE_PAGE_SIZE);	        
			$images=$this->image->findAll(array("id, picTitle, picName,pid"),$page->getOffset(), PICTURE_PAGE_SIZE, "where pid={$pid}");
			$this->assign("pics", $images);
			$this->assign("fpage", $page->getPage());
			$this->assign("path", GALLERY_PATH);
			$this->display("admin/image/list.tpl");	
		}

		function openwin(){
			$this->assign("picPath",GALLERY_PATH);
		
			
			$image=$this->image->find($_GET["id"]);

			$info=getimagesize(GALLERY_REAL_PATH.$image["picName"]);

			if($image["hasThumb"]){
				$newPic=GALLERY_REAL_PATH."th_".$image["picName"];
				$infoH=getimagesize($newPic);
			}
			$this->assign("pic", $image);
			$this->assign("picInfo",$info);
			$this->assign("thpic", $infoH);
			$this->display("admin/image/picinfo.tpl");
		}
		
		
		
		//修改图片
		function mod($tmess=null,$status=1){
			if(empty($tmess)){
				$this->assign("tmess", '提示: 带<span class="red_font">*</span>的项目为必填信息.<br>可以上传的图片格式为GIF,JPEG和PNG,大小不能超过2M.  ');
			}else{
				$this->message($status,$tmess);
			}
		
			if(!$status)
				$image=$_POST;
			else
				$image=$this->image->find($_REQUEST["id"]);

			$select=$this->album->buildSelect("pid", $image["pid"]);
			$this->assign("select", $select);
			if($image["hasThumb"]){
				$this->assign("hasThumb", "checked");
			}

			if($image["hasMark"]){
				$this->assign("hasMark", "checked");
			}
			
			pr(GALLERY_PATH);
			$this->assign("picPath",GALLERY_PATH);
			$this->assign("post", $image);
			$this->display("admin/image/mod.tpl");	
		}

		function update(){
			if($this->image->modPic($_POST)){
				$this->mod($this->image->getMessList(),1);
			}else{
				$this->mod($this->image->getMessList(),0);
			}
		}


	}
